﻿Public Class ClassKMLOut
    Public colorType As Integer = 9


    Public Sub KML22Out(ByVal KMLFileName As String, ByRef rings() As IClassTypeRing)

        Dim filewrite = My.Computer.FileSystem.OpenTextFileWriter(KMLFileName, False, System.Text.Encoding.UTF8)
        Dim tmpNode As TypeNode
        filewrite.WriteLine("<?xml version=""1.0"" encoding=""UTF-8""?>")
        filewrite.WriteLine("<kml xmlns=""http://www.opengis.net/kml/2.2"">")
        filewrite.WriteLine("<Document> ")

        For Each tmpRing As IClassTypeRing In rings
            If tmpRing.isMinRing Then
                filewrite.WriteLine("<Placemark> ")
                filewrite.WriteLine("<name>" & tmpRing.getRingNo & "</name>   ")
                filewrite.WriteLine("<Polygon> ")
                filewrite.WriteLine("<outerBoundaryIs> ")
                filewrite.WriteLine("<LinearRing> ")
                filewrite.WriteLine("<coordinates> ")
                For Each tmpNode In tmpRing.getNodeList
                    filewrite.WriteLine(" " & Str(tmpNode.longitude) & "," & Trim(Str(tmpNode.latitude)) & "," & Trim(Str(tmpNode.altitude)) & " ")
                Next
                '末一个节点
                tmpNode = tmpRing.getStartNode
                filewrite.WriteLine(" " & Str(tmpNode.longitude) & "," & Trim(Str(tmpNode.latitude)) & "," & Trim(Str(tmpNode.altitude)) & " ")


                filewrite.WriteLine("</coordinates> ")
                filewrite.WriteLine("</LinearRing> ")
                filewrite.WriteLine("</outerBoundaryIs> ")
                filewrite.WriteLine("</Polygon> ")
                filewrite.WriteLine("</Placemark> ")
            End If
        Next

        filewrite.WriteLine("</Document> ")
        filewrite.WriteLine("</kml> ")
        filewrite.Flush()
        filewrite.Dispose()

    End Sub

    Public Sub KML22OutLineString(ByVal KMLFileName As String, ByRef rings() As IClassTypeRing)
        '使用线链接，来形成KML数据
        Dim nodelist As IList(Of TypeNode) = New List(Of TypeNode)


        Dim filewrite = My.Computer.FileSystem.OpenTextFileWriter(KMLFileName, False, System.Text.Encoding.UTF8)
        Dim tmpNode As TypeNode
        Dim i As Integer = 0
        filewrite.WriteLine("<?xml version=""1.0"" encoding=""UTF-8""?>")
        filewrite.WriteLine("<kml xmlns=""http://www.opengis.net/kml/2.2"">")
        filewrite.WriteLine("<Document> ")
        filewrite.WriteLine(getColorLineStyle(colorType)) '线形

        For Each tmpRing As IClassTypeRing In rings
            If tmpRing.isMinRing Then
                filewrite.WriteLine("<Placemark> ")
                filewrite.WriteLine("<name>" & tmpRing.getRingNo & "</name>   ")
                filewrite.WriteLine("<styleUrl>#STYLE" & Trim(Str(i Mod colorType)) & "</styleUrl>")

                'filewrite.WriteLine("<Polygon> ")
                'filewrite.WriteLine("<outerBoundaryIs> ")
                filewrite.WriteLine("<LineString> ")
                filewrite.WriteLine("<coordinates> ")
                For Each tmpNode In tmpRing.getNodeList
                    filewrite.WriteLine(" " & Str(tmpNode.longitude) & "," & Trim(Str(tmpNode.latitude)) & "," & Trim(Str(tmpNode.altitude)) & " ")


                    If nodelist.Contains(tmpNode) Then
                    Else
                        nodelist.Add(tmpNode)
                    End If

                Next
                '末一个节点
                tmpNode = tmpRing.getStartNode
                filewrite.WriteLine(" " & Str(tmpNode.longitude) & "," & Trim(Str(tmpNode.latitude)) & "," & Trim(Str(tmpNode.altitude)) & " ")


                filewrite.WriteLine("</coordinates> ")
                filewrite.WriteLine("</LineString> ")
                'filewrite.WriteLine("</outerBoundaryIs> ")
                'filewrite.WriteLine("</Polygon> ")
                filewrite.WriteLine("</Placemark> ")
                i = i + 1
            End If
        Next

        '点坐标
        For Each tmpNode In nodelist
            filewrite.WriteLine("<Placemark> ")
            filewrite.WriteLine("<name>" & tmpNode.NodeName & "</name>   ")


            filewrite.WriteLine("		<LookAt> ")
            filewrite.WriteLine("<longitude>" & Str(tmpNode.longitude) & "</longitude> ")
            filewrite.WriteLine("<latitude>" & Trim(Str(tmpNode.latitude)) & "</latitude> ")
            filewrite.WriteLine("<altitude>" & Trim(Str(tmpNode.altitude)) & "</altitude> ")

            filewrite.WriteLine("</LookAt> ")


            filewrite.WriteLine("<styleUrl>#placemark_circle</styleUrl> ")
            filewrite.WriteLine("<Point> ")
            filewrite.WriteLine("<coordinates>" & Trim(Str(tmpNode.longitude)) & "," & Trim(Str(tmpNode.latitude)) & "," & Trim(Str(tmpNode.altitude)) & "</coordinates> ")
            filewrite.WriteLine("</Point> ")
            filewrite.WriteLine("</Placemark> ")
        Next

        filewrite.WriteLine("</Document> ")
        filewrite.WriteLine("</kml> ")
        filewrite.Flush()
        filewrite.Dispose()

    End Sub

    Private Function getColorLineStyle(Optional ByVal colorType As Integer = 9) As String
    '根据颜色种类，来形成线的颜色
        getColorLineStyle = ""
        Dim strColorLineStyle = ""
        If colorType <= 0 Then
            colorType = 9
        End If
        For i = 0 To colorType
            strColorLineStyle = strColorLineStyle & "<Style id=""STYLE" & Trim(Str(i)) & """>"
            strColorLineStyle = strColorLineStyle & "<LineStyle>"

            strColorLineStyle = strColorLineStyle & "<color>7f" & getColorString(i) & "</color>"
            strColorLineStyle = strColorLineStyle & "<width>2</width>"

            strColorLineStyle = strColorLineStyle & "</LineStyle>"
            strColorLineStyle = strColorLineStyle & "</Style>"
        Next

        getColorLineStyle = strColorLineStyle

    End Function
    Private Function getColorString(ByVal colorType As Integer) As String
        getColorString = "000000"
        Dim pscolor As String = ""
        Select Case (colorType Mod 3)
            Case 0
                pscolor = "00"
            Case 1
                pscolor = "7f"

            Case Else
                pscolor = "ff"
        End Select

        Select Case (Int(colorType / 3) Mod 3)
            Case 0
                pscolor = pscolor & "00"
            Case 1
                pscolor = pscolor & "7f"

            Case Else
                pscolor = pscolor & "ff"
        End Select


        Select Case (Int(colorType / 9) Mod 3)
            Case 0
                pscolor = pscolor & "00"
            Case 1
                pscolor = pscolor & "7f"

            Case Else
                pscolor = pscolor & "ff"
        End Select
        getColorString = pscolor
    End Function


End Class
